package com.issac.vhr.controller;

import com.issac.vhr.entity.Hr;
import com.issac.vhr.entity.RespBean;
import com.issac.vhr.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import utils.HrUtils;

import java.util.Map;

@RestController
@RequestMapping("/hr/info")
public class HrInfoController {

    @Autowired
    HrService hrService;
    @GetMapping
    public Hr getCurrentHrInfo(){
        return HrUtils.getCurrentHr();
    }
    @PutMapping
    public RespBean updateHrInfo(@RequestBody Hr hr, Authentication authentication){
        if (hrService.updateHr(hr)==1){
            //更新currentHr信息
            HrUtils.updateCurrentHr(hr,authentication);
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @PutMapping("/pass")
    public RespBean updateHrPassByHrId(@RequestBody Map<String,Object> map){
        //校验旧密码是否正确
        //更新密码
        String oldPass = (String) map.get("oldPass");
        String pass = (String) map.get("pass");
        Integer hrid = (Integer) map.get("id");
        if (hrService.updateHrPassByHrId(pass,oldPass,hrid)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败",",请确认旧密码是否正确");
    }
}
